package com.liucxu.zimuzu.View.Fragement;


import android.graphics.Color;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.liucxu.zimuzu.Base.BaseFragment;
import com.liucxu.zimuzu.Model.Bean.ResourceDetailsInfoBean;
import com.liucxu.zimuzu.Presenter.IUpdatePresenter;
import com.liucxu.zimuzu.Presenter.Impl.UpdatePresenterImpl;
import com.liucxu.zimuzu.R;
import com.liucxu.zimuzu.View.Adapter.UpdateAdapter;
import com.liucxu.zimuzu.View.IView.IUpdateView;

import java.util.ArrayList;
import java.util.List;

/**
 * 今日更新Fragment
 */
public class UpdateFragment extends BaseFragment implements IUpdateView {

    private IUpdatePresenter updatePresenter;
    private SwipeRefreshLayout swipe_refresh;
    private ListView content_list;
    private UpdateAdapter adapter;
    private ArrayList<ResourceDetailsInfoBean> datas;
    private RelativeLayout no_data;
    private ImageView no_data_iv;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_hot;
    }

    @Override
    public void setupView() {
        swipe_refresh = (SwipeRefreshLayout) $(R.id.swipe_refresh);
        swipe_refresh.setColorSchemeColors(Color.parseColor("#0f88eb"));
        content_list = (ListView) $(R.id.content_list);
        no_data=(RelativeLayout)$(R.id.no_data);
        no_data_iv=(ImageView)$(R.id.no_data_iv);
        datas = new ArrayList<>();
        adapter = new UpdateAdapter(getActivity(), datas);
        content_list.setAdapter(adapter);
        no_data_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                no_data.setVisibility(View.GONE);
                swipe_refresh.setVisibility(View.VISIBLE);
                setupData();
            }
        });
        swipe_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                setupData();
            }
        });
    }

    @Override
    public void setupData() {
        updatePresenter = new UpdatePresenterImpl(this);
        updatePresenter.getUpdateToday();
    }

    @Override
    public void showLoading() {
        swipe_refresh.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        swipe_refresh.setRefreshing(false);
    }

    @Override
    public void showError(String errMsg) {
        Snackbar.make(swipe_refresh,errMsg,Snackbar.LENGTH_SHORT).show();
    }

    @Override
    public void setUpdates(List<ResourceDetailsInfoBean> datas) {
        if (datas != null) {
            adapter.setDatas(datas);
            if(datas.size()==0){
                Snackbar.make(swipe_refresh,"暂时未有更新",Snackbar.LENGTH_SHORT).show();
                no_data.setVisibility(View.VISIBLE);
                swipe_refresh.setVisibility(View.GONE);
            }
            else{
                no_data.setVisibility(View.GONE);
                swipe_refresh.setVisibility(View.VISIBLE);
            }
        }
    }
}
